package com.ln.service.impl;

import com.ln.dao.DeptDao;
import com.ln.entity.Dept;
import com.ln.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author Ln
 * @version 终极版
 * @date 2022/12/7 15:24
 */
@Service
public class DeptServiceImpl2 implements DeptService {
    @Autowired
    private DeptDao deptDao;

//    @Autowired
//    private RedisTemplate<String,Object> redisTemplate;


    @Override
    @Cacheable(cacheNames="redis::dept",key = "#deptno")
    public Dept findById(int deptno) {
        Dept dept = deptDao.selectById(deptno);
        return dept;
    }

    @Override
    public Dept save(Dept dept) {
        deptDao.insert(dept);
        return dept;
    }

    @Override
    @CacheEvict(cacheNames = "redis::dept",key = "#deptno")
    public int delete(int deptno) {
        int i = deptDao.deleteById(deptno);
        return i;
    }

    @Override
    @CachePut(cacheNames = "redis::dept",key = "#dept.deptno")
    public Dept update(Dept dept) {
        deptDao.updateById(dept);
        return dept;
    }
}
